﻿using IJhbyService;
using JhbyCommon.CommonModel;
using JhbyHelper;
using JhbyWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace JhbyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FuelController : ControllerBase
    {
        jhbyrbacContext _context = null;
        //注入
        IFuelService _fuelService;
        public FuelController(IFuelService fuelService)
        {
            _fuelService = fuelService;
        }
        /// <summary>
        /// 烧结粉列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetSinteringList")]
        public string GetSinteringList(int pageIndex, int pageSize)
        {
            Response response1 = new Response();
            //token验证 
            int verifytokenid = TokenHelper.VerifyToken(HttpContext.Request);
            response1.Code = verifytokenid;
            if (verifytokenid == 0)
            {
                response1.data = _fuelService.GetSinteringList(pageIndex, pageSize);
            }
            else if (verifytokenid == 1)
            {
                response1.message = "访问用户不存在！";
            }
            else if (verifytokenid == 2)
            {
                response1.message = "Token验证失败！";
            }
            else
            {
                response1.message = "账号已经在其他地方登陆！";
            }
            return JsonConvert.SerializeObject(response1);
        }


        /// <summary>
        /// 烧结粉新增
        /// </summary>
        /// <param name="sinteringpowder"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("AddSintering")]
        public string AddSintering([FromHeader] Sinteringpowder sinteringpowder)
        {
            Response response1 = new Response();
            var ms = new MemoryStream();
            this.HttpContext.Request.EnableBuffering();
            this.HttpContext.Request.Body.CopyToAsync(ms).GetAwaiter().GetResult();
            var json = System.Text.Encoding.UTF8.GetString(ms.ToArray());
            sinteringpowder = Newtonsoft.Json.JsonConvert.DeserializeObject<Sinteringpowder>(json);
            //token验证 
            int verifytokenid = TokenHelper.VerifyToken(HttpContext.Request);
            response1.Code = verifytokenid;
            if (verifytokenid == 0)
            {
                response1.data = _fuelService.AddSintering(sinteringpowder);
            }
            else if (verifytokenid == 1)
            {
                response1.message = "访问用户不存在！";
            }
            else if (verifytokenid == 2)
            {
                response1.message = "Token验证失败！";
            }
            else
            {
                response1.message = "账号已经在其他地方登陆！";
            }
            return JsonConvert.SerializeObject(response1);
        }
        /// <summary>
        /// 烧结粉删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("DelSintering")]
        public string DelSintering(int Id)
        {
            Response response1 = new Response();
            //token验证 
            int verifytokenid = TokenHelper.VerifyToken(HttpContext.Request);
            response1.Code = verifytokenid;
            if (verifytokenid == 0)
            {
                response1.data = _fuelService.DelSintering(Id);
            }
            else if (verifytokenid == 1)
            {
                response1.message = "访问用户不存在！";
            }
            else if (verifytokenid == 2)
            {
                response1.message = "Token验证失败！";
            }
            else
            {
                response1.message = "账号已经在其他地方登陆！";
            }
            return JsonConvert.SerializeObject(response1);
        }

    }
}
